package com.tdk.bts.view;

import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.naming.spi.ObjectFactory;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.tdk.bts.action.FilmAction;
import com.tdk.bts.entity.Film;
import com.tdk.bts.util.Page;
import com.tdk.bts.util.Request;

public class FilmListView extends JPanel{

	private FilmAction filmAction = new FilmAction();
	
	private Page<Film> page;
	
	private JFrame jf;
	
	public FilmListView(JFrame jf) {
		super();
		this.jf = jf;
		setStyle();
		loadData(1);
	}

	private void setStyle() {
		this.setBounds(0,0,400,600);
		this.setLayout(null);
	}

	private void loadData(int pageNo)
	{
		Request request = new Request();
		request.setParameter("pageNo", pageNo);
		
		try {
			filmAction.findAllFilms(request);
			page = (Page<Film>) request.getAttribute("filmList");
			
			this.removeAll();
			
			for (int i = 0; i < page.size(); i++) {
				FilmItemView fiv = new FilmItemView(page.get(i),jf);
				fiv.setBounds(0, i*135+2, 400, 135);
				this.add(fiv);
			}
			this.repaint();
			this.validate();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage());
			e.printStackTrace();
		}
	}

	public void prePage() {
		loadData(page.getPrePage());
	}

	public void nextPage() {
		System.out.println("page:"+page);
		loadData(page.getNextPage());
	}
	
	
}
